package com.gbits.app.finance.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gbits.app.finance.domain.CostOrgSubjectMapper;
import com.gbits.app.finance.service.CostOrgSubjectMapperService;
import com.gbits.app.finance.service.DefaultAbstractService;

@Service("costOrgSubjectMapperService")
@Transactional
public class CostOrgSubjectMapperServiceImpl extends
		DefaultAbstractService<CostOrgSubjectMapper> implements
		CostOrgSubjectMapperService {

	public CostOrgSubjectMapper get(String orgCode, String costOrgName) {
		
		String hql = " from " + CostOrgSubjectMapper.class.getName() + " e where e.orgCode=:orgCode and e.costOrgName=:costOrgName and e.type=:type";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orgCode", orgCode);
		map.put("costOrgName", costOrgName);
		map.put("type", 1);
		List<CostOrgSubjectMapper> list = super.getBaseDao().findByNamesParameter(hql, map);
		
		if (null == list || list.isEmpty()) {
			throw new RuntimeException(String.format("未找到匹配的配制[成本中心与科目][%s,%s]", orgCode, costOrgName));
		}
		
		if (list.size() == 1) {
			return list.get(0);
		} 
		
		throw new RuntimeException(String.format("找到多个匹配的配制[成本中心与科目][%s,%s]", orgCode, costOrgName));
	}

}
